﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Phoenix.Service;
using Phoenix.Service.Contracts;
using Phoenix.Service.Model;
using Phoenix.Common.Util;
using Phoenix.Enum;
namespace Phoenix.AdminSite.Controllers
{
    public class NewsController : ControllerBase
    {
        INewsCatalogService INewsCatalog = new NewsCatalogService();
        INewsService INews = new NewsService();
        IIDCreaterService IIDCreater = new IDCreaterService();

        public ActionResult Index()
        {

            ViewData["CatalogList"] = INewsCatalog.GetByParentID(0);

            int totalRecord = 0;

            ViewData["NewsList"] = INews.GetPagedList(10, 1, 0, out totalRecord);

            return View();
        }

        public ActionResult Catalog()
        {
            ViewData["CatalogList"] = INewsCatalog.GetByParentID(0);
            ViewData["parentID"] = 0;
            return View();
        }

        public ActionResult CatalogEdit()
        {
            if (!string.IsNullOrEmpty(Request.QueryString["pid"]))
            {
                int pid = int.Parse(Request.QueryString["pid"]);

                ViewData["NewsCatalogList"] = INewsCatalog.GetByParentID(pid);

            }

            if (!string.IsNullOrEmpty(Request.QueryString["cid"]))
            {
                int cid = int.Parse(Request.QueryString["cid"]);

                NewsCatalogModel model = INewsCatalog.GetByCatalogID(cid);
                ViewData["NewsCatalogModel"] = model;
                ViewData["NewsCatalogList"] = INewsCatalog.GetByParentID(model.ParentID);
            }

            return View();
        }

        public ActionResult CatalogSave(FormCollection collection)
        {
            NewsCatalogModel model = new NewsCatalogModel();
            model.CatalogName = collection["txtCategoryName"];
            model.ParentID = int.Parse(collection["sltParentCategoryID"]);
            model.SortOrder = int.Parse(collection["txtSort"]);
            model.CreateTime = DateTime.Now;
            model.CreateUser = 0;
            model.DisplayFlag = true;
            model.IDPath = "";
            model.IsLeaf = true;
            model.Memo = "";
            model.NamePath = "";
            model.NewsCount = 0;
            model.UpdateTime = DateTime.MinValue;
            model.UpdateUser = 0;


            if (!string.IsNullOrEmpty(collection["hdCatalogID"]))
            {
                model.CatalogID = int.Parse(collection["hdCatalogID"]);
                model.UpdateTime = DateTime.Now;
                INewsCatalog.UpdateByCatalogID(model);
            }
            else
            {
                INewsCatalog.AddNewsCatalog(model);

            }

            return ShowMsg("成功", Common.ActionStatus.OK, "");
        }

		[ValidateInput(false)]
        public ActionResult Save(FormCollection collection, string hdaction)
        {
            NewsModel model = new NewsModel();
            bool isAdd = true;
            if ((ActionType)int.Parse(hdaction) == ActionType.Modify)
            {
                int newsID = int.Parse(collection["hdnewsid"]);
                model = INews.GetByNewsID(newsID);
                isAdd = false;
            }

            model.CatalogID = int.Parse(collection["sltCatalogID"]);
            model.Content = collection["txtContent"];
            model.Title = collection["txtTitle"];
			model.DisplayFlag = collection["IsShow"] == "" ? true : false;
            model.UpdateTime = DateTime.Now;
            model.UpdateUser = 1;
            model.Keywords = collection["txtKeywords"];
            model.Description = collection["txtDescription"];

            if (isAdd)
            {
                model.NewsID = IIDCreater.GetNewID("NewsID", "资讯编号", 1000);
                model.CreateTime = DateTime.Now;
                model.CreateUser = 1;
                INews.AddNews(model);
            }
            else
            {
                INews.UpdateByNewsID(model);
            }


            return null;
        }

        public ActionResult Edit()
        {
            int newsID = 0;
            if (!string.IsNullOrEmpty(Request.QueryString["nid"]))
            {
                newsID = int.Parse(Request.QueryString["nid"]);

                NewsModel objNews = INews.GetByNewsID(newsID);

                ViewData["NewsModel"] = objNews;
            }

            ViewData["CatalogList"] = INewsCatalog.GetByParentID(0);

            return View();
        }

		public ActionResult Delete(int newsId)
		{
			INews.DeleteByNewsID(newsId);

			return ShowMsg("删除成功", Common.ActionStatus.OK, "index");
		}

    }
}
